package Servlet;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;

/**
 * @author 张孜轩
 * @date 2022/3/12 14:40
 */
@WebServlet("/login")
public class Login extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String user = req.getParameter("user");
        String password = req.getParameter("password");

        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","niit01");

        HTable table = new HTable(conf, "student1");
        Scan scan = new Scan();
        scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("user"));
        Get get = new Get("001".getBytes());
        get.setMaxVersions(10);
        ResultScanner scanner = table.getScanner(scan);
        Result rs = table.get(get);

        String t = "0";
        for (Result res : scanner) {
            for (KeyValue kv : res.raw()) {
                byte[] user1 = res.getValue("cf".getBytes(), "user".getBytes());
                byte[] password1 = rs.getValue("cf".getBytes(), "password".getBytes());
                if (user.equals(Bytes.toString(user1))  && password.equals(Bytes.toString(password1))) {
                    resp.sendRedirect("/test.html");
                    t = "1";
                }
            }
        }if (t == "0"){
            JOptionPane.showConfirmDialog(null,"账号或密码错误","警告!",JOptionPane.DEFAULT_OPTION);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

